#!/bin/bash

export KIBANA_HOME=${KIBANA_HOME:-"/opt/cloudera/parcels/kibana-7.16.3-1.cdh6.3.2/lib/kibana"}
echo "KIBANA_HOME: ${KIBANA_HOME}"
chown -R kibana:kibana ${KIBANA_HOME}

export KIBANA_LOGS_PATH=(`cat kibana-conf.properties | grep 'logging.path=' | sed 's/^[^=]*=//'`)

export KIBANA_CONF_PATH=${CONF_DIR}/ka-conf
if [ ! -d "${KIBANA_CONF_PATH}" ];then
	mkdir ${KIBANA_CONF_PATH}
else
	rm -rf ${KIBANA_CONF_PATH}/*
fi
export KBN_PATH_CONF=$KIBANA_CONF_PATH
export KIBANA_PATH_CONF=$KIBANA_CONF_PATH

echo "KIBANA_CONF_PATH: ${KIBANA_CONF_PATH}"

init_config() {
    txt=""
    if [[ "${OSTYPE}" == "darwin"* ]]; then
        # Mac OSX
        txt="''"
    fi
   
    HOST_NAME=(`hostname`)
	HOST_ADDR=(`ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:"`)

	cp -r ${KIBANA_HOME}/config/node.options ${KIBANA_CONF_PATH}/node.options

	if [ ! -d "${KIBANA_LOGS_PATH}" ];then
		mkdir ${KIBANA_LOGS_PATH}
	fi

	chown -R kibana:kibana /var/lib/kibana

	ENCRYPTION_KEY=(`echo $RANDOM |md5sum |head -c 32`)
	TMP_ES_HOSTS=(`cat kibana-conf.properties | grep 'elasticsearch.hosts=' | sed 's/^[^=]*=//'`)
	MAX_BACKUP_INDEX=(`cat log4j-ka.properties | grep 'max.log.file.backup.index=' | sed 's/^[^=]*=//'`)
	MAX_FILE_SIZE=(`cat log4j-ka.properties | grep 'max.log.file.size=' |sed 's/^[^=]*=//'`)
	TMP_FILE_SIZE=${MAX_FILE_SIZE//MB/}
	LOG_EVERY_BYTES=$((TMP_FILE_SIZE*1024*1024))
	ELASTICSEARCH_HOSTS="[\"${TMP_ES_HOSTS//,/\",\"}\"]"
   
    echo "server.host: ${HOST_ADDR}" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "pid.file: ${KIBANA_CONF_PATH}/kibana.pid" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "logging.dest: ${KIBANA_LOGS_PATH}/kibana.log" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "logging.rotate.enabled: true" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "logging.timezone: Asia/Shanghai" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "logging.json: false" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "logging.rotate.everyBytes: ${LOG_EVERY_BYTES}" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "logging.rotate.keepFiles: ${MAX_BACKUP_INDEX}" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "xpack.encryptedSavedObjects.encryptionKey: ${ENCRYPTION_KEY}" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "xpack.security.encryptionKey: ${ENCRYPTION_KEY}" >> ${KIBANA_CONF_PATH}/kibana.yml
    echo "xpack.reporting.encryptionKey: ${ENCRYPTION_KEY}" >> ${KIBANA_CONF_PATH}/kibana.yml
   
    while IFS= read -r line; 
	do 
	    echo ${line%=*}": "${line#*=} >> ${KIBANA_CONF_PATH}/kibana.yml
	done < kibana-conf.properties
   
    sed -i ${txt} "s#elasticsearch.hosts: .*#elasticsearch.hosts: ${ELASTICSEARCH_HOSTS}#g" ${KIBANA_CONF_PATH}/kibana.yml
    sed -i ${txt} "/logging.path:/d" ${KIBANA_CONF_PATH}/kibana.yml
   
    chown -R kibana:kibana ${CONF_DIR}
    chown -R kibana:kibana ${KIBANA_LOGS_PATH}
}

start_ka() {
    echo "Start Kibana"
    init_config
	RUN_USER=kibana
    exec runuser -l $RUN_USER -c "export KBN_PATH_CONF=${KIBANA_CONF_PATH} && export KBN_PATH_CONF=${KIBANA_CONF_PATH} && exec ${KIBANA_HOME}/bin/kibana > ${KIBANA_LOGS_PATH}/kibana.out 2>&1"
}
 
stop_ka() {
    echo "Stop Kibana"
    pid=$(ps -ef | grep "${KIBANA_HOME}/bin" | grep -v grep | awk '{print $2}')
    if [ -z "$pid" ]
    then
	    echo "no kibana to stop"
    else
	    for pid in ${pid[*]}
	    do
	        echo "stopping kibana $pid"
		    kill $pid
		    sleep 5
		    if kill -0 $pid > /dev/null 2>&1; then
			    echo "kibana did not stop gracefully after 5 seconds: killing with kill -9"
			    kill -9 $pid
		    fi
	    done
	    sleep 5
    fi
}

case "$1" in
    start)
        start_ka
        ;;
    stop)
        stop_ka
        ;;
    restart)
        stop_ka
        start_ka
        ;;
    *)
        echo "Usage kibana {start|stop|restart}"
        exit 2
        ;;
esac